library(lterdatasampler)
vertebrates <- and_vertebrates |>
select(year, section, unittype, species, length_1_mm, weight_g) |>
filter(species != "Cascade torrent salamander")
vertebratesDay 2 - Introduction to Data Analysis with R
Freie Universität Berlin - Theoretical Ecology
February 28, 2025
Example plots you can create with ggplot
Data set and_vertebrates with measurements of a trout and 2 salamander species in different forest sections.
year: observation yearsection: CC (clear cut forest) or OG (old growth forest)unittype: channel classification (C = Cascade, P = Pool, …)species: Species measuredlength_1_mm: body length [mm]weight_g: body weight [g]Data set and_vertebrates with measurements of a trout and 2 salamander species in different forest sections.
#> # A tibble: 32,191 × 6
#> year section unittype species length_1_mm weight_g
#> <dbl> <chr> <chr> <chr> <dbl> <dbl>
#> 1 1987 CC R Cutthroat trout 58 1.75
#> 2 1987 CC R Cutthroat trout 61 1.95
#> 3 1987 CC R Cutthroat trout 89 5.6
#> 4 1987 CC R Cutthroat trout 58 2.15
#> 5 1987 CC R Cutthroat trout 93 6.9
#> 6 1987 CC R Cutthroat trout 86 5.9
#> 7 1987 CC R Cutthroat trout 107 10.5
#> 8 1987 CC R Cutthroat trout 131 20.6
#> 9 1987 CC R Cutthroat trout 103 9.55
#> 10 1987 CC R Cutthroat trout 117 13
#> # ℹ 32,181 more rows
ggplot(data)The ggplot() function initializes a ggplot object. Every ggplot needs this function.
aes(x, y)The aesthetics define how data variables are mapped plot properties.
aes(x, y)The aesthetics define how data variables are mapped plot properties.
geom_*geoms define how data points are represented. There are many different geoms to chose from
geom_point+data and aes defined in ggplot call are inherited to all plot layers#> Warning: Removed 13270 rows containing missing values or values outside the scale range
#> (`geom_point()`).
geom_pointaes(color): mapping color to a variableLooks like there are two groups of data: Map color of points to a variable by adding it to aesthetics:
aes(size): mapping size to a variableWe can do the same with size:
aes(shape): mapping shape to a variableWe can do the same with shape:
We can also combine these aesthetics and map different variables
The scales onto which the aesthetic elements are mapped can be changed.
scale_x_log10The scales onto which the aesthetic elements are mapped can be changed.
geom_smoothAdd a smoothing line that helps see patterns in the data
method = "lm", a linear regression line is addedgeom_boxplotCompare groups using a boxplot
geom_boxplotCompare groups using a boxplot
geom_boxplotMap the unittype to the color aesthetic of the boxplot
geom_boxplotMap the unittype to the fill aesthetic of the box
geom_histogramgeom_histogramgeom_tileYou can create a simple heatmap with geom_tile
facet_wrapSplit your plots along one variable with facet_wrap
facet_gridSplit your plots along two variables with facet_grid
Task 1.1 - 1.2 (45 min)
Exploratory data analysis with the penguin data set
Find the task description here
(Don’t do the “Beautify” task)
We can also save a plot in a variable
scale_color_viridis_dChange the colors of the color aesthetic:
scale_color_manualWe can also manually specify colors:
You can use the paletteer package to access color scales from many packages.
scale_color_paletteer_d for discrete and scale_color_paletteer_c for continuous color scalesscale_fill_* vs. scale_color_*labs: Change axis and legend titles and add plot titlelabs: Change axis and legend titles and add plot titletheme_*: change appearanceggplot2 offers many pre-defined themes that we can apply to change the appearance of a plot.
theme_*: change appearanceggplot2 offers many pre-defined themes that we can apply to change the appearance of a plot.
theme(): customize themeYou can manually change a theme or even create an entire theme yourself. The elements you can control in the theme are:
If you want a full list of what you can customize, have a look at
theme(): customize themeTo edit a theme, just add another theme() layer to your plot.
theme_set(): set global themeYou can set a global theme that will be applied to all ggplot objects in the current R session.
Add this to the beginning of your script.
ggsave()A ggplot object can be saved on disk in different formats.
Without specifications:
Task 2 (30 min)
Make your penguin plots more beautiful
Find the task description here
Selina Baldauf // Data visualization with ggplot2